﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TESTCommons.JsonHelper;
using TestModel.SysManage;
using TestService.SysManage;
using TESTCommons.ConvertTpye;
using TestManage.Infrastructure.CustomAttribute;
using System.Collections;
using Microsoft.AspNetCore.Http;
using TESTCommons.Encrypt;

namespace TestManage.Controllers
{
    [AopActionFilter(true, false)]
    public class SysUserController : BaseController
    {
        #region 参数
        private readonly ISys_UserService _sys_UserService;
        #endregion

        #region 构造函数
        public SysUserController(ISys_UserService sys_UserService)
        {
            _sys_UserService = sys_UserService;
        }
        #endregion

        [HttpGet]
        public override IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult GetUsers(IFormCollection fc, int page = 0, int rows = 20)
        {
            var query = FormCollectionToHashtable(fc);
            if (page > 0)
                page = page - 1;
            var data = _sys_UserService.QueryUser(query, page, rows);
            return Json(new { total = data.TotalCount, rows = data });
        }

        /// <summary>
        /// 保存
        /// </summary>
        [HttpPost]
        public IActionResult Save(sys_User model)
        {
            if (string.IsNullOrEmpty(model.userLoginName))
            {
                return Json(new AjaxResult { type = ResultType.error, errorcode = 1, message = "账号不能为空" });
            }
            if (string.IsNullOrEmpty(model.userPwd))
            {
                return Json(new AjaxResult { type = ResultType.error, errorcode = 4, message = "密码不能为空" });
            }
            if (string.IsNullOrEmpty(model.userEmail))
            {
                return Json(new AjaxResult { type = ResultType.error, message = "Email不能为空" });
            }
            if (string.IsNullOrEmpty(model.userName))
            {
                return Json(new AjaxResult { type = ResultType.error, message = "用户名不能为空" });
            }

            model.userPwd = MD5Provider.Hash(model.userPwd);
            string mess = "";
            var data = _sys_UserService.GetModel(model.gid);
            if (data != null)
            {
                data.finallyTime = DateTime.Now;
                data.userLoginName = model.userLoginName;
                data.userName = model.userName;
                data.userPwd = model.userPwd;
                data.userEmail = model.userEmail;
                _sys_UserService.UpdateUser(data);
                mess = "修改成功";
            }
            else
            {
                var hashtable = new Hashtable();
                hashtable.Add("userLoginName", model.userLoginName);
                var user = _sys_UserService.QueryUser(hashtable).FirstOrDefault();
                if (user != null)
                {
                    if (user.userEmail == model.userEmail)
                    {
                        return Json(new AjaxResult { type = ResultType.error, errorcode = 2, message = "邮编已经存在" });
                    }
                    if (user.userName == model.userName)
                    {
                        return Json(new AjaxResult { type = ResultType.error, errorcode = 3, message = "用户名已经存在" });
                    }
                    return Json(new AjaxResult { type = ResultType.error, errorcode = 1, message = "账号已经存在" });
                }
                model.gid = Guid.NewGuid();
                model.isDelete = false;
                model.isManage = false;
                model.userIsActivate = false;
                model.createTime = model.finallyTime = DateTime.Now;
                _sys_UserService.SignIn(model);
                mess = "添加成功";
            }
            return Json(new AjaxResult { type = ResultType.success, message = mess });
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public IActionResult Delete(string Ids)
        {
            _sys_UserService.DeleteData(Ids);
            return Json(new AjaxResult { type = ResultType.success, message = "修改成功" });
        }

        /// <summary>
        /// 查询根据ID
        /// </summary>
        [HttpPost]
        public IActionResult LoadForm(string ID)
        {
            var model = _sys_UserService.GetModel(ID.ToGuid());
            if (model == null)
            {
                return Json(new AjaxResult { type = ResultType.error, message = "用户不存在" });
            }
            return Json(model);
        }
    }
}
